package com.bw.controller.orders;



import com.bw.entity.orders.PiOrderPic;
import com.bw.resp.ResponseResult;
import com.bw.service.orders.IPiOrderPicService;
import com.bw.utils.idwork.IdWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;


import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lizhengqian
 * @since 2020-06-19
 */
@RestController

@RequestMapping("order-pic")
public class PiOrderPicController {


    @Autowired
    private IPiOrderPicService orderPicService;

    private final Logger logger=LoggerFactory.getLogger(PiOrderPicController.class);

    @Value("${filePath}")
    private String filePath;
    @Autowired
    private IdWorker idWorker;

//    @PostMapping("/upload/{code}")
//    public ResponseResult upload(MultipartFile[] file,@PathVariable Long code){
//        logger.info("上传开始");
////        ResponseResult result = ResponseResult.SUCCESS();
////        try {
////            for (MultipartFile f:file){
//////                String filename = f.getOriginalFilename();//文件名称
//////                String itemCode=filename.substring(0,filename.lastIndexOf("."));//图号
//////                String ext = filename.substring(filename.lastIndexOf("."), filename.length());
//////
//////                File dirfile1 = new File(filePath);
//////                if(!dirfile1.exists()){
//////                    dirfile1.mkdir();
//////                }
//////                new PiOrderP;
////
////                File file1 = new File(filePath + code + f.getOriginalFilename());
////                f.transferTo(file1);
////                PiOrderPic orderPic = new PiOrderPic();
////                orderPic.setItemId(code);
////                orderPic.setCode(f.getOriginalFilename().substring(0,f.getOriginalFilename().lastIndexOf(".")));
////                orderPic.setName(f.getOriginalFilename().substring(0,f.getOriginalFilename().lastIndexOf(".")));
////                orderPic.setFilePath(filePath+code+f.getOriginalFilename());
////                orderPic.setFileSize(String.valueOf(f.getSize()));
////                orderPicService.saveOrUpdate(orderPic);
////
////
////            }
////            result.setMessage("图片上传成功");
////        }catch (Exception e){
////            logger.error("上传失败"+e.toString());
////            result.setSuccess(false);
////            result.setMessage("上传失败"+e.toString());
////        }
////        logger.info("上传结果");
////        return result;
////    }

    @PostMapping("/upload")
    public ResponseResult upload(@RequestParam("file") MultipartFile file, Long code){
        ResponseResult result=ResponseResult.SUCCESS();
        try {
            logger.info("上传开始");
            orderPicService.importPic(file,code);
        }catch (Exception e){
            logger.error("上传失败"+e.toString());
            result=ResponseResult.FAIL();
        }
        return  result;

    }
    @GetMapping("/preview/{itemId}/{code}")
    public ResponseResult preview(@PathVariable("itemId") Long itemId, @PathVariable("code")String code, HttpServletResponse response){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            InputStream is=orderPicService.preview(itemId,code);
            int len = is.available();
            byte[] buffer = new byte[1024];
            ServletOutputStream outputStream = response.getOutputStream();
            while ((len=is.read(buffer))!=-1){
                outputStream.write(buffer,0,len);
            }
            outputStream.close();
        }catch (Exception e){
            e.printStackTrace();
            result = ResponseResult.FAIL();
            result.setResult("没有找到对应的图纸");
        }
        return result;
    }
}
